package com.jp.factoy.controller;


import com.jp.factoy.pojo.FactoryAddVo;
import com.jp.factoy.pojo.FactorySelectVo;
import com.jp.factoy.pojo.FactoryStateVo;
import com.jp.factoy.pojo.FactoryUpdateVo;
import com.jp.factoy.service.FactoryService;
import com.jp.factoy.util.AllRecords;
import com.jp.factoy.util.ResultBuildVo;
import com.jp.factoy.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by Administrator on 2019/6/13.
 */
@RestController
@RequestMapping("/test")
public class FactoryController {
    @Autowired
    private FactoryService factoryService;
    /*
   * 新增
   * */
    @PostMapping("/addFactory")
    public ResultVo addFactory(@RequestBody @Valid FactoryAddVo factoryAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){

            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return factoryService.addFactory(factoryAddVo);
    }
    /*
*修改
* */
    @PostMapping("/updateFactory")
    public ResultVo updateFactory(@RequestBody @Valid FactoryUpdateVo factoryUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){

            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return factoryService.updateFactory(factoryUpdateVo);
    }
    /**
     * 修改状态 启动，静止状态
     */
    @PostMapping("/updateState")
    public ResultVo updateState(@RequestBody @Valid FactoryStateVo factoryStateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return factoryService.updateState(factoryStateVo);
    }
    /*
* 查询
* */
    @GetMapping("/queryFactoryById")
    public ResultVo queryFactoryById(@RequestParam("fid") Integer fid){

        return factoryService.queryFactoryById(fid);
    }
    /*
    * 分页
    * */
    @PostMapping("/queryPage")
    public ResultVo<AllRecords> queryPage(@RequestBody FactorySelectVo factorySelectVo){
        return factoryService.queryPage(factorySelectVo);
    }



}